package main

import (
	"errors"
	"fmt"
	"strconv"
)

/*
	做一个数组队列，最简单的实现
		数组初始时，front为0，rear为0
					front永远指头元素，rear指向尾元素的后一个元素
 */

type Queue struct {
	front int
	rear int
	arr [3]int
	maxSize int
}

func (q *Queue) addQueue (val int) (err error) {
	if q.maxSize == q.rear {
		err = errors.New("队列已满")
		return
	}
	q.arr[q.rear] = val
	q.rear++
	return
}

func (q *Queue) getQueue () (data int, err error) {
	if q.front == q.rear {
		err = errors.New("队列为空")
		return
	}
	data = q.arr[q.front]
	q.arr[q.front] = 0
	q.front++
	return
}

func (q *Queue) showQueue (){
	for i, k := range q.arr {
		fmt.Printf("arr[%d]=%d\t", i, k)
		if i == q.maxSize-1 {
			fmt.Println()
		}
	}
}

func main(){
	q := Queue{
		front:   0,
		rear:    0,
		arr:     [3]int{},
		maxSize: 3,
	}
	var flag = true
	for flag{
		fmt.Println("使用队列：")
		fmt.Println("输入add添加元素")
		fmt.Println("输入get获取元素")
		fmt.Println("输入show展示队列")
		fmt.Println("输入exit退出")
		var commend string
		var value string
		_, _ = fmt.Scanln(&commend)
		switch commend {
		case "add":
			fmt.Print("输入要添加的元素:")
			_, _ = fmt.Scanln(&value)
			data, err := strconv.Atoi(value)
			if err != nil {
				fmt.Println("请输入正整数")
			}else{
				err = q.addQueue(data)
				if err != nil {
					fmt.Println("err=",err)
				}
			}
		case "get":
			data,err := q.getQueue()
			if err != nil {
				fmt.Println("err=",err)
			}else{
				fmt.Println("data=", data)
			}
		case "show":
			q.showQueue()
		case "exit":
			flag = false
		default:
			fmt.Println("请输入正确的选项")
		}
	}
}

